package com.openmanus.agent.core;

/**
 * 智能体状态枚举
 * 定义了智能体的各种运行状态
 * 
 * @author OpenManus Team
 * @version 1.0.0
 */
public enum AgentState {
    
    /**
     * 空闲状态 - 智能体未在执行任务
     */
    IDLE("空闲"),
    
    /**
     * 运行状态 - 智能体正在执行任务
     */
    RUNNING("运行中"),
    
    /**
     * 思考状态 - 智能体正在分析和规划
     */
    THINKING("思考中"),
    
    /**
     * 行动状态 - 智能体正在执行具体操作
     */
    ACTING("行动中"),
    
    /**
     * 等待状态 - 智能体正在等待外部响应
     */
    WAITING("等待中"),
    
    /**
     * 暂停状态 - 智能体执行被暂停
     */
    PAUSED("已暂停"),
    
    /**
     * 错误状态 - 智能体执行出现错误
     */
    ERROR("错误"),
    
    /**
     * 完成状态 - 智能体任务执行完成
     */
    COMPLETED("已完成"),
    
    /**
     * 终止状态 - 智能体被强制终止
     */
    TERMINATED("已终止");

    private final String description;

    AgentState(String description) {
        this.description = description;
    }

    /**
     * 获取状态描述
     * 
     * @return 状态描述
     */
    public String getDescription() {
        return description;
    }

    /**
     * 判断是否为活跃状态
     * 
     * @return 是否活跃
     */
    public boolean isActive() {
        return this == RUNNING || this == THINKING || this == ACTING || this == WAITING;
    }

    /**
     * 判断是否为终端状态
     * 
     * @return 是否为终端状态
     */
    public boolean isTerminal() {
        return this == COMPLETED || this == TERMINATED || this == ERROR;
    }

    /**
     * 判断是否可以转换到目标状态
     * 
     * @param targetState 目标状态
     * @return 是否可以转换
     */
    public boolean canTransitionTo(AgentState targetState) {
        // 定义状态转换规则
        return switch (this) {
            case IDLE -> targetState == RUNNING || targetState == TERMINATED;
            case RUNNING -> targetState == THINKING || targetState == ACTING || 
                           targetState == COMPLETED || targetState == ERROR || 
                           targetState == PAUSED || targetState == TERMINATED;
            case THINKING -> targetState == ACTING || targetState == WAITING || 
                            targetState == ERROR || targetState == TERMINATED;
            case ACTING -> targetState == THINKING || targetState == WAITING || 
                          targetState == COMPLETED || targetState == ERROR || 
                          targetState == TERMINATED;
            case WAITING -> targetState == THINKING || targetState == ACTING || 
                           targetState == ERROR || targetState == TERMINATED;
            case PAUSED -> targetState == RUNNING || targetState == TERMINATED;
            case ERROR -> targetState == IDLE || targetState == TERMINATED;
            case COMPLETED -> targetState == IDLE;
            case TERMINATED -> targetState == IDLE;
        };
    }

    @Override
    public String toString() {
        return description;
    }
}
